package com.ews.onlineexchange.service.schedule;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.TradeStatus;
import com.ews.onlineexchange.service.BulletinService;
import com.ews.onlineexchange.service.SmsService;
import com.ews.onlineexchange.service.UnoveredAndPassedBulletinList;
import com.ews.onlineexchange.service.UnoveredBulletinList;

@Named
@Singleton
@Startup
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class BulletinOverTimer {
	
	@Resource
	TimerService timerService;
	@Inject
	EntityManager entityManager;
	@Inject
	BulletinService bulletinEndService;
	
	@Inject
	UnoveredBulletinList unoveredBulletinList;
	@Inject
	UnoveredAndPassedBulletinList unoveredAndPassedBulletinList;
	@Inject
	SmsService smsService;
	
	@PostConstruct
	public void init() {
		List<Bulletin> resultList = getUnoveredBulletinList();
		Collection<Timer> timers = timerService.getAllTimers();
		for (Timer timer : timers) {
			if(timer.isPersistent()) {
				if(timer.getInfo() != null && timer.getInfo().toString().startsWith("com.ews.onlineexchange.model.Bulletin")) {
					timer.cancel();
				}
			}
		}
		for (Bulletin bulletin : resultList) {			
			timerService.createTimer(bulletin.getChooseTime(), bulletin);
		}
		List<Bulletin> unoveredAndPassedBulletins = unoveredAndPassedBulletinList.getResultList();
		for (Bulletin bulletin : unoveredAndPassedBulletins) {
			bulletin.setTradeStatus(TradeStatus.Breaked);
			bulletin.setOver(true);
			entityManager.merge(bulletin);
		}
		
	}
	
	public List<Timer> getAllTimers() {
		return new ArrayList<>(timerService.getAllTimers());
	}
	
	public String registerNewBulletin(Bulletin bulletin) {
		Timer timer = timerService.createTimer(bulletin.getChooseTime(), bulletin);		
		System.out.println("======================================================");
		System.out.println(timer.isPersistent());
		System.out.println("======================================================");
		return "registered";
	}
	
	/*当手动开始时，取消已生成的timer*/
	public void cancelBulletinTimer(Bulletin bulletin) {
		Collection<Timer> timers = timerService.getAllTimers();
		for (Timer timer : timers) {
			if(timer.getInfo() != null && timer.getInfo().toString().startsWith("com.ews.onlineexchange.model.Bulletin")) {
				Bulletin b = (Bulletin) timer.getInfo();
				if(b.getId().equals(bulletin.getId())) {
					timer.cancel();
				}
			}
			
		}
	}
	
	@Timeout
	public void timeout(Timer timer) {
		Bulletin bulletin = (Bulletin) timer.getInfo();
		if(bulletin.getTradeStatus().equals(TradeStatus.Opened)) {
			bulletinEndService.endBulletin(bulletin);
			
		}
		
//		smsService.addMessageForSupplier(bulletin);
//		smsService.addMessageForBuyer(bulletin);
	}

	private List<Bulletin> getUnoveredBulletinList() {
		return unoveredBulletinList.getResultList();
	}
	
}
